<?php

/**
 * This is the model class for table "estudiopaciente".
 *
 * The followings are the available columns in table 'estudiopaciente':
 * @property integer $idEstudioPaciente
 * @property integer $idPaciente
 * @property string $fechaEstudio
 * @property string $nroProtocolo
 */
class Estudiopaciente extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Estudiopaciente the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'estudiopaciente';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nroProtocolo, idPaciente, fechaEstudio', 'required'),
			array('idPaciente', 'numerical', 'integerOnly'=>true),
			array('nroProtocolo', 'length', 'max'=>40),
			array('fechaEstudio', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idEstudioPaciente, idPaciente, fechaEstudio, nroProtocolo', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idEstudioPaciente' => 'Id Estudio Paciente',
			'idPaciente' => 'Paciente',
			'fechaEstudio' => 'Fecha Estudio',
			'nroProtocolo' => 'Nro Protocolo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idEstudioPaciente',$this->idEstudioPaciente);
		$criteria->compare('idPaciente',$this->idPaciente);
		$criteria->compare('fechaEstudio',$this->fechaEstudio,true);
		$criteria->compare('nroProtocolo',$this->nroProtocolo,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function getEstudios($etapa){
		
		$model=new Tipoestudio();
		$estudios = $model->findAllByAttributes(array('idEtapa'=>$etapa));
		
		return  $estudios;
		
	}
	
	public function getDetalleEstudioInstances($idEstudioPaciente, $idEtapa){

		
		$tipoEstudios = $this->getEstudios($idEtapa);
		$detalleEstudioModel = new Detalleestudiopaciente();
		
		$detalleEstudios = array();
		
		foreach ($tipoEstudios as $tipoEstudio){
			
			$currentDetalleEstudios = $detalleEstudioModel->findAllByAttributes(array('idEstudioPaciente'=>$idEstudioPaciente,'idTipoEstudio'=>$tipoEstudio->idTipoEstudio));
			
			if (count($currentDetalleEstudios) != 0){
				
				$newDetalleEstudio = $currentDetalleEstudios[0];
				$newTipoEstudio = $tipoEstudio->findByAttributes(array('idTipoEstudio'=>$newDetalleEstudio->idTipoEstudio));
				$newDetalleEstudio->tipoEstudio = $newTipoEstudio;
								
			}else{
				
				$newDetalleEstudio = new Detalleestudiopaciente();
				$newDetalleEstudio->idEstudioPaciente = $idEstudioPaciente;
				
				$newDetalleEstudio->idTipoEstudio = $tipoEstudio->idTipoEstudio;
				$newDetalleEstudio->tipoEstudio = $tipoEstudio;
				
			}
			array_push($detalleEstudios, $newDetalleEstudio);
			
		}
		
		return $detalleEstudios;
		
	}
	
	public function getDetalleEstudios($limit=5){
		
		//$where = "status = 'Played' AND (`home_team_id`='".$team."' OR `away_team_id`='".$team."') ";
 
		$where = "";
		
		$dataProvider=new CActiveDataProvider('DetalleEstudios',
		array(
		'criteria'=>array(
			'condition'=>$where,
			'order'=>'nroDeEstudio DESC',
		),
		'pagination'=>array(
		'pageSize'=>$limit,
		),
		));
		 

		//$where = "status = 'Played' AND (`home_team_id`='".$team."' OR `away_team_id`='".$team."') ";
 		/*
		$dataProvider=new CActiveDataProvider('DetalleEstudios',
		array(
		'criteria'=>array(
		'condition'=>$where,
		'order'=>'date_ts DESC',
		),
		'pagination'=>array(
		'pageSize'=>$limit,
		),
		));
		
		*/
		
		
		
		
		return $dataProvider;
		
	}
}